<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, maximum-scale = 1, minimum-scale=1" />
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta name="viewport" content="target-densitydpi=device-dpi,width=640,user-scalable=0">
		<title>车栈-注册</title>
		<link href="css/style.css" rel="stylesheet" type="text/css" />

		<script src="js/jquery-1.8.2.min.js"></script>
		<script src="js/mui.min.js"></script>
		<script src="js/app.js"></script>
	</head>

	<body>
		<section class="section">
			<div class="top-gray">
				<a href="" class="back"><img src="images/back-gray.png"></a>
				<a class="pro_t">注册</a>
			</div>
			<div class="clear"></div>
			<div class="pos-top">
				<form id="reg" class="login" name="" method="post" action="">
					<div class="zhmm">
						<label class="user">手机号</label>
						<input type="tel" id="phone" class="yzm" placeholder="请输入真实手机号" />
						<input type="button" readonly="readonly" class="hqyzm" id="send" value="获取验证码">
					</div>
					<div class="zhmm">
						<label class="user">验证码</label>
						<input type="text" id="code" class="name01" size="20" maxlength="50" placeholder="请输入验证码" /> </div>
					<div class="zhmm">
						<label class="user">密码</label>
						<input type="password" id="password" class="name01" size="20" maxlength="50" placeholder="请输入密码" /></div>
					<div class="zhmm">
						<label class="user">确认密码</label>
						<input type="password" id="password2" class="pwd" size="20" maxlength="50" placeholder="请再次确认密码" /></div>
					<input type="button" class="butl" id="sub" value="立即注册">

					<p>点击注册表明您同意
						<a href="">车栈服务条款</a>
					</p>

				</form>
			</div>

		</section>
	</body>

</html>
<script>
	mui.init({

	});
	mui.plusReady(function() {
		//如果已经登陆，传入用户名

		//跳转页面。加入了判断登陆得功能
		mui("body").on("tap", ".gopage", function() {

			var this_obj = jQuery(this);
			var url = this_obj.data("url");
			var id = this_obj.data("param");
			var login = this_obj.data("login");
			if(login == 1) {
				//说明必须要验证登录
				var if_login = plus.storage.getItem("if_login");
				if(if_login != 1) {
					url = "login.html";
				}

			}
			//  mui.toast(id);
			mui.openWindow({
				id: url,
				url: url,
				extras: {
					data_id: id,

				},
				waiting: {
					autoShow: false,
				}
			})
		});

	})
	//点击注册
	var sub = document.getElementById("sub");

	sub.addEventListener('tap', function(event) {
		var code = $("#code").val();
		var phone = $("#phone").val();
		var password = $("#password").val();
		var password2 = $("#password2").val();

		if(!checkphone(phone)) {
			mui.toast("请输入正确的手机号");
		} else if(code.length < 4) {
			mui.toast("请输入正确的验证码");
		} else if(password.length < 6) {
			mui.toast("密码长度不得少于6位");
		} else if(password != password2) {
			mui.toast("两次密码不一致");

		} else {

			var postData = {
				apiUrl: web_url + "index/customer_register.shtml",
				datas: {
					phone: phone,
					code:code,
					password:password
				}
			};

			app.postServer(postData,
				function(result) {
						plus.storage.setItem("if_login", "1");
						plus.storage.setItem("name", result.user.name);
						plus.storage.setItem("password", result.user.password);
						plus.storage.setItem("user_id", result.user.id+ "");
						console.log(result.user.id);
					
						var main;
						var web = plus.webview.getWebviewById("main");
						if(web == null) {
							web = plus.webview.create("main.html", "main", "", "");
							web.show();
						} else {
							web.show();
						}

				},
				function(err) {
					if(err) {
						plus.nativeUI.toast(err);
						return;
					}
				}
			);


		}

	}, false);

	var send = document.getElementById("send");

	send.addEventListener('tap', function(event) {
		var phone = $("#phone").val();
		if(checkphone(phone)) {
			settime();
			var postData = {
				apiUrl: web_url + "index/send_code.shtml",
				datas: {
					phone: phone
				}
			};

			app.postServer(postData,
				function(result) {
					mui.toast(result.code);

				},
				function(err) {
					if(err) {
						plus.nativeUI.toast(err);
						return;
					}
				}
			);

		} else {
			mui.toast("请输入正确的手机号码");

		}

	}, false);
	var countdown = 60;

	function settime() {
		if(countdown == 0) {
			$("#send").removeattr("disabled");
			$("#send").val("获取验证码");
			countdown = 60;
			}
			else {
				$("#send").attr("disabled", true);
				$("#send").val('重新发送(' + countdown + ')');
				countdown--;
			}
			setTimeout(function() {
				settime();
			}, 1000)
		}

		function checkphone(phone) {

			var myreg = /^(((17[0-9]{1})|(13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
			if(!myreg.test(phone)) {

				return false;
			} else {
				return true;
			}

		}
</script>